package edu.tyut.game.ui.game

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import edu.tyut.game.R
import edu.tyut.game.logic.model.Msg

class MsgAdapter(private val msgList: List<Msg>) : RecyclerView.Adapter<MsgViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        if (viewType == Msg.TYPE_RECEIVED)
            LeftViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.msg_received_item, parent, false))
        else  RightViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.msg_send_item, parent, false))

    override fun onBindViewHolder(holder: MsgViewHolder, position: Int) {
        val msg = msgList[position]
        // 密封类
        when(holder){
            is LeftViewHolder -> holder.leftMsg.text = msg.content
            is RightViewHolder -> holder.rightMsg.text = msg.content
        }
    }

    override fun getItemCount() = msgList.size

    override fun getItemViewType(position: Int): Int {
        val msg = msgList[position]
        return msg.type
    }
}